package org.loofer.ext.utils;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.content.Context;
import android.os.Build.VERSION;
import android.os.Debug;
import android.text.format.Formatter;
import cn.finalteam.toolsfinal.io.FileUtils;
import org.loofer.ext.component.logger.Logger;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileReader;

public final class MemoryUtils {
    private static final String TAG = "MemoryUtils";
    private static volatile Long sSystemTotalMemory;

    private MemoryUtils() {
    }

    public static long getSystemTotalMemory(Context context) {
        if (sSystemTotalMemory != null) {
            return sSystemTotalMemory.longValue();
        }
        synchronized (MemoryUtils.class) {
            if (sSystemTotalMemory != null) {
                long longValue = sSystemTotalMemory.longValue();
                return longValue;
            }
            Long valueOf = Long.valueOf(obtainSystemTotalMemory(context));
            sSystemTotalMemory = valueOf;
            longValue = valueOf.longValue();
            return longValue;
        }
    }

    public static long getSystemAvailableMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService("activity");
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);
        return mi.availMem;
    }

    private static long obtainSystemTotalMemory(Context context) {
        Throwable e;
        Throwable th;
        long totalMem = 0;
        if (VERSION.SDK_INT >= 16) {
            ActivityManager am = (ActivityManager) context.getSystemService("activity");
            MemoryInfo mi = new MemoryInfo();
            am.getMemoryInfo(mi);
            return mi.totalMem;
        }
        Closeable reader = null;
        try {
            Closeable reader2 = new BufferedReader(new FileReader("/proc/meminfo"), 8192);
            try {
                String memLine = reader2.readLine();
                if (memLine != null) {
                    totalMem = (long) (Integer.valueOf(memLine.split("\\s+")[1]).intValue() * 1024);
                }
                IoUtils.closeSilently(reader2);
                return totalMem;
            } catch (Throwable th2) {
                th = th2;
                reader = reader2;
                IoUtils.closeSilently(reader);
                throw th;
            }
        } catch (Throwable th3) {
            e = th3;
            Logger.i(TAG, "fail to obtain system total memory", e);
            IoUtils.closeSilently(reader);
            return 0;
        }
    }

    @SuppressLint({"NewApi"})
    public static String getMemoryStats(Context context) {
        StringBuilder text = new StringBuilder();
        try {
            text.append("\ntotalMemory()=").append(toMib(context, Runtime.getRuntime().totalMemory()));
            text.append("\nmaxMemory()=").append(toMib(context, Runtime.getRuntime().maxMemory()));
            text.append("\nfreeMemory()=").append(toMib(context, Runtime.getRuntime().freeMemory()));
            Debug.MemoryInfo memInfo = new Debug.MemoryInfo();
            Debug.getMemoryInfo(memInfo);
            text.append("\ndalvikPrivateDirty=").append(toMib(memInfo.dalvikPrivateDirty));
            text.append("\ndalvikPss=").append(toMib(memInfo.dalvikPss));
            text.append("\ndalvikSharedDirty=").append(toMib(memInfo.dalvikSharedDirty));
            text.append("\nnativePrivateDirty=").append(toMib(memInfo.nativePrivateDirty));
            text.append("\nnativePss=").append(toMib(memInfo.nativePss));
            text.append("\nnativeSharedDirty=").append(toMib(memInfo.nativeSharedDirty));
            text.append("\notherPrivateDirty=").append(toMib(memInfo.otherPrivateDirty));
            text.append("\notherPss").append(toMib(memInfo.otherPss));
            text.append("\notherSharedDirty=").append(toMib(memInfo.otherSharedDirty));
            text.append("\ntotalPrivateDirty=").append(toMib(memInfo.getTotalPrivateDirty()));
            text.append("\ntotalPss=").append(toMib(memInfo.getTotalPss()));
            text.append("\ntotalSharedDirty=").append(toMib(memInfo.getTotalSharedDirty()));
        } catch (Throwable e) {
            Logger.d(TAG, "fail to get memory stats", e);
        }
        return text.toString();
    }

    public static void logMemoryStats(Context context) {
        Logger.i(TAG, getMemoryStats(context));
    }

    private static String toMib(int size) {
        return String.format("%.2fMB", new Object[]{Double.valueOf(((double) size) / FileUtils.KB)});
    }

    private static String toMib(Context context, long nativeHeapSize) {
        return Formatter.formatFileSize(context, nativeHeapSize);
    }
}
